package com.yangjun.dao.impl.realtimeLiveAnalysis.realtimeSource;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement.Else;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.realtimesource.ChartDefine;
import com.yangjun.entity.realtimeliveAnalysis.KeywordInforEntity;
import com.yangjun.entity.realtimeliveAnalysis.RegionInforEntity;

@Repository
public class RealtimeListImpl {
	
	 @Autowired
	 @Resource(name = "bikadJdbcTemplate")
	 private JdbcTemplate mJdbcTemplate;
	 
	 public Map<String, Object> DepartmentBarChart(String platform, String name, String legend, String xAxis, String yAxis) {
		 	Map<String, Object> resultMap = Maps.newHashMap();
		 	
		 	String platformSQL = "";
		 	String orderAmountSQL = "";
		 	String orderCntSQL = "";
		 	
		 	if (StringUtils.isNotBlank(platform))
		 		platformSQL = " and ordersource in (" + platform + ") ";
		 	
			String sql =
						" select " + xAxis + " as xAxis, "
						+"        yAxis,"
						+"        round(yAxis / total_order_amount, 4) as rate"
						+"   from (select " + xAxis + ", round(sum(" + yAxis + "), 0) as yAxis"
						+"           from dv_dw_kad_live_ware"
						+ "			 where DATA_DATE = trunc(sysdate)"
						+			 platformSQL
						+"          group by " + xAxis + ""
						+"          order by yAxis) A,"
						+"        (select sum(" + yAxis + ") as total_order_amount"
						+"           from dv_dw_kad_live_ware"
						+ "			where DATA_DATE = trunc(sysdate)"
						+ 			platformSQL
						+ "		  ) B";
	        
			System.out.println("sql === " + sql);
	        List<Map<String,Object>> queryList = mJdbcTemplate.queryForList(sql);
	        
	        // 柱状图数据
	        String[] amountArr = new String[queryList.size()];
	        String[] platformArr = new String[queryList.size()];
	        String[] rateArr = new String[queryList.size()];
	        for(int i=0, len=queryList.size(); i<len; i++) {
	            amountArr[i] = String.valueOf(queryList.get(i).get("yAxis"));
	            platformArr[i] = String.valueOf(queryList.get(i).get("xAxis"));
	            rateArr[i] = String.valueOf(queryList.get(i).get("rate"));
	        }
	        resultMap.put("value", amountArr);
	        resultMap.put("department", platformArr);
	        resultMap.put("rate", rateArr);
	        resultMap.put("name", name);
	        resultMap.put("legend", legend);
	        
	        return resultMap;
	 }
	 
	 
	 /**
	  * 营销套餐详情
	  * @param kitcode
	  * @param platform
	  * @return
	  */
	 public List<Map<String,Object>> operatingDetailWare(String kitcode, String platform) {
		 String platformSQL = "";
		 
		if (StringUtils.isNotBlank(platform))
		 	platformSQL = " and ordersource in (" + platform + ") ";
		 	
		String sql = " select A.KITCODE,"
					+"		  A.warecode,"
					+"        A.warename,"
					+"        A.order_cnt,"
					+"        A.ORDER_AMOUNT, "
					+"        pkg_visual_consign.util_division(A.order_amount,B.total_order_amount, 4) as rate "
					+"   from (select warecode, warename,KITCODE, sum(order_cnt) as order_cnt, sum(ORDER_AMOUNT) as ORDER_AMOUNT "
					+"           from dv_dw_kad_live_ware_group"
					+"			where DATA_DATE = trunc(sysdate) and ISKIT = 1 "			
					+"          and kitcode = '" + kitcode + "'"
					+ 			platformSQL
					+"          group by warecode, warename,KITCODE) A,"
					+"        (select sum(order_amount) as total_order_amount"
					+"           from dv_dw_kad_live_ware_group"
					+"			where DATA_DATE = trunc(sysdate) and ISKIT = 1"			
					+"          and kitcode = '" + kitcode + "'"
					+			platformSQL
					+ "			) B ";
		
//		System.out.println("flag--"+sql);
		
		List<Map<String,Object>> queryList = mJdbcTemplate.queryForList(sql);
		
		return queryList;
	 }
	 
	 /**
	  * 热销商品组合详情
	  * @param kitcode
	  * @param platform
	  * @return
	  */
	 public List<Map<String,Object>> HostDetailWare(String kitcode, String platform) {
		 String platformSQL = "";
		 
		 if (StringUtils.isNotBlank(platform))
			 platformSQL = " and ordersource in (" + platform + ") ";
		 
		 String sql = " select A.KITCODE,"
				 +"		   A.warecode,"
				 +"        A.warename,"
				 +"        A.order_cnt,"
				 +"        A.ORDER_AMOUNT,"
				 +"        pkg_visual_consign.util_division(A.order_amount,B.total_order_amount, 4) as rate"
				 +"   from (select warecode, warename,KITCODE, sum(order_cnt) as order_cnt, sum(ORDER_AMOUNT) as ORDER_AMOUNT"
				 +"           from dv_dw_kad_live_ware_group"
				 +"			where DATA_DATE = trunc(sysdate) and ISKIT = 0 "			
				 +"          and WAREGROUP = '" + kitcode + "'"
				 + 			platformSQL
				 +"         group by warecode, warename,KITCODE ) A,"
				 +"        (select sum(order_amount) as total_order_amount"
				 +"           from dv_dw_kad_live_ware_group"
				 +"			where DATA_DATE = trunc(sysdate) and ISKIT = 0 "			
				 +"          and WAREGROUP = '" + kitcode + "'"
				 +			platformSQL
				 + "			) B ";
		 
		 System.out.println("flag22--"+sql);
		 List<Map<String,Object>> queryList = mJdbcTemplate.queryForList(sql);
		 
		 return queryList;
	 }
}















